/**
(c) Copyright 2008 Hewlett-Packard Development Company, L.P.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

package com.hp.gagawa.builder.java;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class Tag {
	private String name_;
	private String capTag_;
	private boolean allowsKids_;
	private ArrayList<String> req_attrs_;
	private ArrayList<String> opt_attrs_;
	private static HashMap<String, String> specialCases_;
	
	private static final String LICENSE = "/**\n(c) Copyright 2008 Hewlett-Packard Development Company, L.P.\n\n" + 
		"Permission is hereby granted, free of charge, to any person obtaining a copy\n" +
		"of this software and associated documentation files (the \"Software\"), to deal\n" +
		"in the Software without restriction, including without limitation the rights\n" +
		"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n" +
		"copies of the Software, and to permit persons to whom the Software is\n" +
		"furnished to do so, subject to the following conditions:\n\n" +
		"The above copyright notice and this permission notice shall be included in\n" +
		"all copies or substantial portions of the Software.\n\n" +
		"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n" +
		"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n" +
		"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n" +
		"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n" +
		"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n" +
		"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n" +
		"THE SOFTWARE.\n*/";
	
	public Tag(String tag, boolean allowsKids, ArrayList<String> req, ArrayList<String> opt){
		if(specialCases_ == null){
			initSpecialCases();
		}
		this.name_ = tag;
		this.allowsKids_ = allowsKids;
		this.req_attrs_ = req;
		this.opt_attrs_ = opt;
		
		capTag_ = specialCases_.get(name_);
		if(capTag_ == null){
			capTag_ = capitalize(name_);
		}
	}
	
	public void writeTagClass() {
		try {
			System.out.println(name_);
			File output = new File("src/com/hp/gagawa/java/elements", capTag_ + ".java");
			PrintWriter out = new PrintWriter(new FileOutputStream(output));
			out.println("/**Generated by the Gagawa TagBuilder " + new Date() + "*/");
			out.println();
			out.println(LICENSE);
			out.println();
			out.println("package com.hp.gagawa.java.elements;");
			out.println();
			out.println(String.format("import com.hp.gagawa.java.%s;",(allowsKids_)?"FertileNode":"Node"));
			out.println();
			if(allowsKids_){
				out.println("import com.hp.gagawa.java.Node;");
				out.println("import com.hp.gagawa.java.elements.Text;");
				out.println("import java.util.List;");
				out.println();
			}
			out.println(String.format("public class %s extends %s {",capTag_, (allowsKids_)?"FertileNode":"Node"));
			out.println();
			out.println(writeConstructor());
			out.println();
			
			if(!allowsKids_){
				out.println(writeWriteMethod());
				out.println();
			}
			
			if(req_attrs_.size() > 0){
				for(String attr: req_attrs_){
					out.println(writeAttributeMethods(attr));
				}
			}
			
			if(allowsKids_)
				out.print(writeAppendMethods());
			
			if(opt_attrs_.size() > 0){
				for(String attr: opt_attrs_){
					out.println(writeAttributeMethods(attr));
				}
			}
			out.println("}");
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String writeConstructor(){
		StringBuffer b = new StringBuffer();
		b.append("\tpublic ");
		b.append(capTag_);
		b.append("(");
		//write params
		int len = req_attrs_.size();
		int i;
		if(len > 0){
			for(i = 0; i < len-1; i++){
				String attr = req_attrs_.get(i);
				if(specialCases_.get(attr) != null){
					attr = specialCases_.get(attr);
				} 
				b.append("String ");
				b.append(attr);
				b.append(", ");
			}
			String attr = req_attrs_.get(i);
			if(specialCases_.get(attr) != null){
				attr = specialCases_.get(attr);
			} 
			b.append("String ");
			b.append(attr);
		}
		b.append("){\n");
		b.append(String.format("\t\tsuper(\"%s\");\n",name_));
		//write setters
		if(len > 0){
			for(i = 0; i < len; i++){
				String attr = req_attrs_.get(i);
				if(specialCases_.get(attr) != null){
					attr = specialCases_.get(attr);
				} 
				b.append("\t\tset");
				b.append(capitalize(attr));
				b.append("(");
				b.append(attr);
				b.append(");\n");
			}
		}
		b.append("\t}\n");
		return b.toString();
	}
	
	private String writeAppendMethods(){
		StringBuilder b = new StringBuilder();
		
		b.append("\t/**\n");
		b.append("\t * Appends a child node to the end of this element's DOM tree\n");
		b.append("\t * @param child node to be appended\n");
		b.append("\t * @return the node\n");
		b.append("\t */\n");
		b.append("\tpublic ").append(capTag_).append(" appendChild(Node child){\n");
		b.append("\t\tif(this == child){\n");
		b.append("\t\t\tthrow new Error(\"Cannot append a node to itself.\");\n");
		b.append("\t\t}\n");
		b.append("\t\tchild.setParent(this);\n");
		b.append("\t\tchildren.add(child);\n");
		b.append("\t\treturn this;\n");
		b.append("\t}\n");
		
		b.append("\t/**\n");
		b.append("\t * Appends a child node at the given index\n");
		b.append("\t * @param index insert point\n");
		b.append("\t * @param child node to be appended\n");
		b.append("\t * @return the node\n");
		b.append("\t */\n");
		b.append("\tpublic ").append(capTag_).append(" appendChild(int index, Node child){\n");
		b.append("\t\tif(this == child){\n");
		b.append("\t\t\tthrow new Error(\"Cannot append a node to itself.\");\n");
		b.append("\t\t}\n");
		b.append("\t\tchild.setParent(this);\n");
		b.append("\t\tchildren.add(index, child);\n");
		b.append("\t\treturn this;\n");
		b.append("\t}\n");
		
		b.append("\t/**\n");
		b.append("\t * Appends a list of children in the order given in the list\n");
		b.append("\t * @param children nodes to be appended\n");
		b.append("\t * @return the node\n");
		b.append("\t */\n");
		b.append("\tpublic ").append(capTag_).append(" appendChild(List<Node> children){\n");
		b.append("\t\tif(children != null){;\n");
		b.append("\t\t\tfor(Node child: children){\n");
		b.append("\t\t\t\tappendChild(child);\n");
		b.append("\t\t\t}\n");
		b.append("\t\t}\n");
		b.append("\t\treturn this;\n");
		b.append("\t}\n");
		
		
		b.append("\t/**\n");
		b.append("\t * Appends the given children in the order given\n");
		b.append("\t * @param children nodes to be appended\n");
		b.append("\t * @return the node\n");
		b.append("\t */\n");
		b.append("\tpublic ").append(capTag_).append(" appendChild(Node... children){\n");
		b.append("\t\tfor(int i = 0; i < children.length; i++){\n");
		b.append("\t\t\tappendChild(children[i]);\n");
		b.append("\t\t}\n");
		b.append("\t\treturn this;\n");
		b.append("\t}\n");
		
		b.append("\t/**\n");
		b.append("\t * Convenience method which appends a text node to this element\n");
		b.append("\t * @param text the text to be appended\n");
		b.append("\t * @return the node\n");
		b.append("\t */\n");
		b.append("\tpublic ").append(capTag_).append(" appendText(String text){\n");
		b.append("\t\treturn appendChild(new Text(text));\n");
		b.append("\t}\n");
		
		b.append("\t/**\n");
		b.append("\t * Removes the child node\n");
		b.append("\t * @param child node to be removed\n");
		b.append("\t * @return the node\n");
		b.append("\t */\n");
		b.append("\tpublic ").append(capTag_).append(" removeChild(Node child){\n");
		b.append("\t\tchildren.remove(child);\n");
		b.append("\t\treturn this;\n");
		b.append("\t}\n");
		
		b.append("\t/**\n");
		b.append("\t * Removes all child nodes\n");
		b.append("\t * @return the node\n");
		b.append("\t */\n");
		b.append("\tpublic ").append(capTag_).append(" removeChildren(){\n");
		b.append("\t\tchildren.clear();\n");
		b.append("\t\treturn this;\n");
		b.append("\t}\n");
		
		return b.toString();
	}
	
	private String writeWriteMethod(){
		StringBuilder b = new StringBuilder();
		b.append("\t@Override\n");
		b.append("\tpublic String write(){\n");
		b.append("\t\treturn writeOpen();\n");
		b.append("\t}\n");
		return b.toString();
	}
	
	private String writeAttributeMethods(String attr) {
		StringBuilder b = new StringBuilder();
		String attrName = specialCases_.get(attr);
		if(attrName == null){
			attrName = capitalize(attr);
		}
		b.append(String.format("\tpublic %s set%s(String value){setAttribute(\"%s\", value); return this;}\n", capTag_, attrName, attr));
		b.append(String.format("\tpublic String get%s(){return getAttribute(\"%s\");}\n", attrName, attr));
		b.append(String.format("\tpublic boolean remove%s(){return removeAttribute(\"%s\");}\n", attrName, attr));
		return b.toString();
	}
	
	private static String capitalize(String token){
		return token.substring(0,1).toUpperCase() + token.substring(1).toLowerCase();
	}
	
	
	private static void initSpecialCases(){
		specialCases_ = new HashMap<String, String>();
		specialCases_.put("class", "CSSClass");
		specialCases_.put("xml:lang", "XMLLang");
		specialCases_.put("http-equiv", "HttpEquiv");
		specialCases_.put("accept-charset", "AcceptCharset");
		specialCases_.put("!--", "Comment");
		specialCases_.put("!DOCTYPE", "Doctype");
	}
}
